package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.LossStudent;
import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.mapper.LossStudentMapper;
import cn.wolfcode.crm.mapper.PotentialStudentMapper;
import cn.wolfcode.crm.query.StudentQuery;
import cn.wolfcode.crm.service.IStudentManagementService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class StudentManagementService implements IStudentManagementService{
    @Autowired
    private PotentialStudentMapper potentialStudentMapper;
    @Autowired
    private LossStudentMapper studentMapper;
    @Override
    public List<PotentialStudent> query(Long id) {
        return potentialStudentMapper.query(id);
    }

    @Override
    public void updateState(Long id,String state) {
        potentialStudentMapper.updateState(id,state);
    }

    @Override
    public List<PotentialStudent> queryList(StudentQuery q) {

        return potentialStudentMapper.queryList(q);
    }

    @Override
    public void insert(PotentialStudent p) {
        potentialStudentMapper.insert(p);
    }

    @Override
    public void update(PotentialStudent student) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String employmentIntention = objectMapper.writeValueAsString(student.getEmploymentIntention());
            student.setEmploymentIntention(employmentIntention);
            potentialStudentMapper.update(student);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void auditLoss(Long id) {
        studentMapper.audit(id);
    }

    @Override
    public void updateLoss(LossStudent s) {
        studentMapper.updateByPrimaryKey(s);
    }


    @Override
    public void updateClass(Long id, String newClassName,String state) {
        Date date = new Date();
        potentialStudentMapper.updateById(id,date);//添加转班时间
        potentialStudentMapper.updateClass(id,newClassName,state);
    }

    @Override
    public void insertLoss(LossStudent s) {
        s.setHandle(UserContext.getUserContext());
        s.setLossDate(new Date());
        studentMapper.insertLoss(s);
    }

    @Override
    public void reverseState(Long id) {
        studentMapper.reverseState(id);
    }

    @Override//正式学员结果集
    public PageResult getPageResult(StudentQuery q) {
        int count = potentialStudentMapper.studentCount(q);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<PotentialStudent> data =potentialStudentMapper.queryList(q);
        return new PageResult(count, data);
    }

    @Override//流失学员结果集
    public PageResult getLossPageResult(StudentQuery q) {
        int count = studentMapper.studentLossCount(q);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<LossStudent> data =studentMapper.selectLossAll(q);
        return new PageResult(count, data);
    }

    @Override
    public PotentialStudent getIdByName(String stringCellValue) {
        return potentialStudentMapper.getIdByName(stringCellValue);
    }



}
